package com.situ.allaia.controller;

import com.situ.allaia.model.Category;
import com.situ.allaia.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/category")
public class CategoryController {
    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/list")
    public String list(){

        return "category/list";
    }
    @GetMapping("/add")
    public String add(Integer id, Map<String,Object> map){
        map.put("id",id);
        return "category/add";
    }
    @GetMapping("/edit")
    public String edit(Integer id,Map<String,Object> map){
        Category category = categoryService.findById(id);
        if (category==null){
            map.put("error","您要修改的类别数据不存在");
            return "category/edit";
        }
        map.put("category",category);
        return "category/edit";
    }

}
